package com.hssoftvn.android.apps.callmessmoneycontroller.modules;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.telephony.SmsManager;

import com.hssoftvn.android.apps.callmessmoneycontroller.util.SUtils;

public class SmsPhoneCallUtills {
	static SmsManager mSmsMan = null;
	static PendingIntent mWaitingIntent = null;
	static PendingIntent mDeliveryIntent = null;
	static WaitingSendingReceiver mWaitingReceiver = null;
	static WaitingDeliveringReceiver mDeliveryReceiver = null;
	
	static int REQUEST_CODE_SENDING_SMS_REPORT = 100;
	static int REQUEST_CODE_DELIVERING_SMS_REPORT = 101;
	
	static int REQUEST_CODE_CALL_PHONE = 200;
	static int REQUEST_CODE_DIAL_PHONE = 201;
	
	static String INTENT_SENDING_SMS_REPORT = "com.hssoftvn.android.TINY_ACTION_SENDING_SMS_REPORT";
	static String INTENT_DELIVERING_SMS_REPORT = "com.hssoftvn.android.TINY_ACTION_DELIVERING_SMS_REPORT";
	
	public static void init(){
		IntentFilter filter = new IntentFilter(INTENT_SENDING_SMS_REPORT);
		Intent intent = new Intent(INTENT_SENDING_SMS_REPORT);		
		int flags = PendingIntent.FLAG_UPDATE_CURRENT;
		mWaitingIntent = PendingIntent.getBroadcast(SUtils.getAppContext(), REQUEST_CODE_SENDING_SMS_REPORT, intent, flags);
		mWaitingReceiver = new WaitingSendingReceiver();
		SUtils.getAppContext().registerReceiver(mWaitingReceiver, filter);
		
		filter = new IntentFilter(INTENT_DELIVERING_SMS_REPORT);
		intent = new Intent(INTENT_DELIVERING_SMS_REPORT);		
		mDeliveryIntent = PendingIntent.getBroadcast(SUtils.getAppContext(), REQUEST_CODE_DELIVERING_SMS_REPORT, intent, flags);
		mDeliveryReceiver = new WaitingDeliveringReceiver();
		SUtils.getAppContext().registerReceiver(mDeliveryReceiver, filter);
		
	}
	
	public static void sendSMS(String to, String message){
		mSmsMan = SmsManager.getDefault();
		mSmsMan.sendTextMessage(to, null, message, mWaitingIntent, mDeliveryIntent);
	}

	static class WaitingSendingReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals(INTENT_SENDING_SMS_REPORT)){
				//SUtils.showDialogMessage("Notice", "Message is sent!");
				SUtils.showToastMessage("Message is sent!");
			}
		}
	}
	
	static class WaitingDeliveringReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals(INTENT_DELIVERING_SMS_REPORT)){
			//SUtils.showDialogMessage("Notice", "Message is delivered!");
				SUtils.showToastMessage("Message is delivered!");
			}
		}
		
	}
	
	public static void callNumber(String to){
		Uri number = Uri.parse("tel:" + to);
        Intent callIntent = new Intent(Intent.ACTION_CALL, number);
        //SUtils.getAppContext().startActivityForResult(callIntent, REQUEST_CODE_CALL_PHONE);
        SUtils.getAppContext().startActivity(callIntent);
	}
	public static void dialNumber(String to){
		Uri number = Uri.parse("tel:" + to);
        Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
        //SUtils.getAppContext().startActivityForResult(callIntent, REQUEST_CODE_DIAL_PHONE);
        SUtils.getAppContext().startActivity(callIntent);
	}
	
	public static void terminate(){
		
		try {
			SUtils.getAppContext().unregisterReceiver(mWaitingReceiver);
			SUtils.getAppContext().unregisterReceiver(mDeliveryReceiver);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
}
